package com.cskaoyan.weixin.service;

import com.cskaoyan.bean.Topic;
import com.cskaoyan.bean.TopicExample;
import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.weixin.bean.topic.WxTopicDetailVO;
import com.cskaoyan.weixin.bean.topic.WxTopicListVO;
import com.cskaoyan.weixin.bean.topic.WxTopicListVO1;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.tomcat.util.descriptor.web.WebXml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxTopicServiceImpl implements WxTopicService {

    @Autowired
    TopicMapper topicMapper;

    @Override
    public WxTopicListVO list(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<WxTopicListVO1> topics = topicMapper.selectWxTopicVO1();
        WxTopicListVO wxTopicListVO = new WxTopicListVO();
        wxTopicListVO.setData(topics);
        PageInfo pageInfo = new PageInfo(topics);
        long total = pageInfo.getTotal();
        wxTopicListVO.setCount(total);
        return wxTopicListVO;
    }

    @Override
    public WxTopicDetailVO detail(Integer id) {
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        WxTopicDetailVO wxTopicDetailVO = new WxTopicDetailVO();
        if (topics.size() != 0) {
            Topic topic = topics.get(0);
            wxTopicDetailVO.setTopic(topic);
            wxTopicDetailVO.setGoods(topic.getGoods());
        }
        return wxTopicDetailVO;
    }

    @Override
    public List<Topic> related(Integer id) {
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andIdNotEqualTo(id);
        List<Topic> topicList = topicMapper.selectByExample(topicExample);
        if (topicList.size() >= 4) {
            topicList = topicList.subList(0, 4);
        }
        return topicList;
    }
}
